package com.xuecheng.content.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author Linzkr
 * @description: TODO
 * @date 2023/2/1 8:50
 */
@Slf4j
public class SecurityUtil {
    public static XcUser getUser(){
        Object userJSON = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        XcUser xcUser = null;
        if (userJSON instanceof  String){
             xcUser = JSON.parseObject((String) userJSON, XcUser.class);
        }
        if (xcUser==null){
            log.debug("解析jwt中的用户无法转换为XCUser对象:{}",userJSON);
        }
        return xcUser;

    }
    @Data
    public static  class XcUser implements Serializable {

        private static final long serialVersionUID = 1L;

        private String id;

        private String username;

        private String password;

        private String salt;

        private String name;
        private String nickname;
        private String wxUnionid;
        private String companyId;
        /**
         * 头像
         */
        private String userpic;

        private String utype;

        private LocalDateTime birthday;

        private String sex;

        private String email;

        private String cellphone;

        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;


    }
}
